var util = require('util');

var ModelManager = require('./ModelManager');
var TaskManager = require('./TaskManager');
var DeviceManager = require('./DeviceManager');
var ViewController = require('./ViewController');

/**
 * 
 * @author jmanero
 * 
 * Bundles all sub-modules of the Control-Server API into an object to pass to
 * custom applications
 * 
 */
var Server = function(callback) {
	server = this; // Global access to server object

	// Start the task manager
	server.taskManager = new TaskManager(function() {
		
	});

	// Connect to the MongoDB and manage models
	server.modelManager = new ModelManager(server, function() {
		this.add('Device');
		this.add('Port');
	});

	// Start the DeviceManager
	server.deviceManager = new DeviceManager(server, function() {
		server.taskManager.add('locateDevices', 'periodic', this.config.locatePeriod, function() {
			server.deviceManager.locate();
		});
	});

	// Start the 
	server.viewController = new ViewController(server, function() {
		this.add('SaveDevice');
		this.add('EditDevice');
		this.add('Devices');
		this.add('SavePort');
		this.add('EditPort');
		this.add('Ports');
		this.add('Dashboard');
	});

	callback(server);
	util.log("Started ControlServer");

	return this;
};

// Server.prototype.addModel = function(name, schema) {
// if(typeof(schema) == 'string') {
// schema = require(this.config.db.modelPath + '/' + schema);
// }
//	
// this.models[name] = this.db.model(name, schema);
// };

exports.createServer = Server;
